<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - alc</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-alc.html">here</a>.</p>


<div id="alc" style="display:none;">
    <b>${query}</b>に該当するALCの記事:
    <div style="clear:both;">
        <u>${title}</u>
        <small>
        <br />
        ${detail}
        </small>
    </div>
</div>

<div id="alcs" style="display:none;">
    <b>${query}</b>に該当するALCの見出し一覧:
    <div style="clear:both;">
        <small>
        <br />
        ${detail}
        </small>
    </div>
</div>

<script class="commands"><![CDATA[

var noun_type_alcentry = {
    _name : "alc entry",
    
    suggest : function(text, html){
        var suggestions = [];

        jQuery.ajax( {
            url: getAlcUrl(text),
            type : "GET",
            dataType: "HTML",
            async: false,
            cache: false,
            success: function(responseData){
                jQuery(responseData).find("#resultList li .midashi").slice(0,5).each(function(){
                    suggestions.push(CmdUtils.makeSugg(jQuery(this).text()));
                });
                if(suggestions.length==0)suggestions.push(CmdUtils.makeSugg(text));
            },
            error: function(text){
                suggestions.push(CmdUtils.makeSugg(text));
            }
        });
        
        return suggestions;
        
    }
};


CmdUtils.CreateCommand({
    name: "alc",
    description: "ALC 英辞郎 on the WEB",
    takes: {search: noun_arb_text},
    icon: "http://www.alc.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "ALC 英辞郎 on the WEB";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を ALC で検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: getAlcUrl(searchText),
          dataType: "html",
          cache: false,
          
          error: function() {
            previewBlock.innerHTML = "Error searching ALC.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            //var title = responseData.find(".midashi").text();
            //var detail = responseData.find("div .wordlink").text();
            var title = responseData.find("#resultList li:first .midashi").text();
            var detail = responseData.find("#resultList li:first DIV").html();
            
            var previewData = {
              query: searchText,
              title: title,
              detail: detail
            };
            
            //Utils.reportInfo(title + detail);
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
                                        jQuery("#alc", feed.dom).html(),
                                        previewData);
            
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( getAlcUrl(directObject.text) );
    }
});

CmdUtils.CreateCommand({
    name: "alc-suggest",
    description: "ALC 英辞郎 on the WEB",
    takes: {search: noun_type_alcentry},
    icon: "http://www.alc.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "ALC 英辞郎 on the WEB";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を ALC で検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: getAlcUrl(searchText),
          dataType: "html",
          cache: false,
          
          error: function() {
            previewBlock.innerHTML = "Error searching ALC.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            var errorMsg = responseData.find("td:has(img[src='http://eowimg.alc.co.jp/content/img/bikkuri.gif'])").html();
            
            //var title = responseData.find(".midashi").text();
            //var detail = responseData.find("div .wordlink").text();
            var title = responseData.find("#resultList li:first .midashi").text();
            var detail = responseData.find("#resultList li:first DIV").html();
            
            var previewData = {
              query: searchText,
              title: title,
              detail: detail
            };
            
            //Utils.reportInfo(title + detail);
            
            if(errorMsg){
                previewBlock.innerHTML = errorMsg;
            }else{
                previewBlock.innerHTML = CmdUtils.renderTemplate(
                                            jQuery("#alc", feed.dom).html(),
                                            previewData);
            }
            
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( getAlcUrl(directObject.text) );
    }
});

CmdUtils.CreateCommand({
    name: "alc-list",
    description: "ALC 英辞郎 on the WEB 見出し語表示",
    takes: {search: noun_arb_text},
    icon: "http://www.alc.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",

    preview: function( previewBlock, directObject ) {

        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "ALC 英辞郎 on the WEB 見出し語表示";
            previewBlock.innerHTML = previewStr;
            return;
        }

        var previewTemplate = "<b>${query}</b> を ALC で検索します...";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        
        CmdUtils.previewAjax(previewBlock, {
          type: "GET",
          url: getAlcUrl(searchText),
          dataType: "html",
          
          error: function() {
            previewBlock.innerHTML = "Error searching ALC.";
          },
          
          success: function(responseData) {
            
            responseData = jQuery(responseData);
            
            var detail = "";
            responseData.find("#resultList li .midashi").each(function(){ detail = detail + jQuery(this).text() + "<br />" } );
            
            var previewData = {
              query: searchText,
              detail: detail
            };
            
            previewBlock.innerHTML = CmdUtils.renderTemplate(
              jQuery("#alcs", feed.dom).html(),
              previewData);
          }
        });

    },

    execute: function( directObject ) {
            Utils.openUrlInBrowser( getAlcUrl(directObject.text) );
    }
});



function getAlcUrl(query){
    var url = "http://eow.alc.co.jp/{QUERY}/UTF-8/";
    url = url.replace(/%s|{QUERY}/g, jQuery.trim(query) );
    return url;
}


]]></script>
</body>
</html>
